<template>
  <div>
    <!-- 成本费用表提报 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @resetChange="resetChange" @onCollapse="collapseHandle"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="qjhCostReportSubmit"
        :height="'auto'"
        :isToperInfo="false"
        :isColumnFilter="false"
        :isSelectColumn="false"
        :columns="columns"
        :dataSource="tableList"
        :onSyncTableData="tableChangeFun"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls">
           <el-button size="small" type="text" style="font-size:14px; font-weight:bold;margin-right:20px;color:red;width: calc(100vw - 420px);text-align: left;">
             <span v-text="`当前状态：${this.currStatus}`"></span>
           </el-button>
          
          <el-button size="small" type="primary" @click="importHandle">导 入</el-button>
          <multiuse-button size="small" type="primary" :disabled="!reportId" :click="exportHandle">导 出</multiuse-button>
        </template>
      </FilterTable>
      <div style="height:30px;"></div>
      <FormPanel ref="formPanel" :list="formList" @formChange="formChangeHandle" :isSubmitBtn="false" :collapse="false" />
      <div
        :style="{
          zIndex: 9,
          borderTop: '1px solid #e9e9e9',
          padding: '10px 20px',
          background: '#fff',
          textAlign: 'center'
        }"
      >
        <multiuse-button size="small" type="primary" v-show="isShow" :click="dataHandle.bind(this, 60791002)">提 交</multiuse-button>
        <multiuse-button size="small" type="primary" v-show="isShow" :click="dataHandle.bind(this, 60791001)">保 存</multiuse-button>
      </div>
    </Spin>

    <BaseDialog :visible.sync="isImport" title="数据导入" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ImportFile :importData="formData" @close="emitCloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import moment from 'moment';
import { getOwnerCode } from '@/assets/js/auth';
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils/index';
import { queryList, exportExcel, saveOne } from '@/api/dealerFinance/CRReportManage/CostReportSubmit';
import { getLoginUser } from '@/api/cms-api/projectOperating/program/departmentInquire'; //获取登录信息
import ImportFile from '../ImportFile';

export default {
  name: 'CostReportSubmit',
  mixins: [dictionary],
  components: {
    ImportFile
  },
  data() {
    this.BaseTable = null;
    this.saveData = null;
    this.conf = { add: '新增', edit: '编辑', show: '明细' };

    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      formList: this.createdFrom1(),
      dealerName: '',
      exportLoading: false,
      isImport: false,
      isShow: true,
      formData: {},
      tableList: [],
      reportId: '',
      year: moment().format('YYYY'),
      month: moment().format('MM'),
      currStatus:'',
    };
  },
  created() {
    this.getDealerName();
    this.getList(moment().format('YYYY'), moment().format('MM'));
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },

  methods: {
    // 获取登录信息
    async getDealerName() {
      const res = await getLoginUser();
      if (res.resultCode == 200) {
        this.dealerName = res.data.dealerName;
      }
    },
    tableChangeFun(val) {
      if (val && val.length > 0) {
        // 设置不可编辑-行列
        let arr = [];
        let [saleSum3, saleSum4, glSum3, glSum4, financeSum3, financeSum4] = [0, 0, 0, 0, 0, 0];

        // 强转数字、设置置灰列；
        val.map((x, index) => {
          arr.push(x);
          if (index > 1) {
            if (index == 2) {
              x.column3 = ' ';
              x.column4 = ' ';
              return;
            }
            // x.column3 = x.column3 || 0;
            // x.column4 = x.column4 || 0;
          }

          if (index > 3 && index < 19) {
            saleSum3 += x.column3 * 10000;
            saleSum4 += x.column4 * 10000;
          }
          if (index > 19 && index < 38) {
            glSum3 += x.column3 * 10000;
            glSum4 += x.column4 * 10000;
          }
          if (index > 38 && index < 42) {
            financeSum3 += x.column3 * 10000;
            financeSum4 += x.column4 * 10000;
          }
        });
        this.BaseTable.SET_CELL_UNEDITABLE(arr, 'column1', true);
        this.BaseTable.SET_CELL_UNEDITABLE(arr, 'column2', true);
        this.BaseTable.SET_CELL_UNEDITABLE([val[0], val[1], val[2], val[3], val[19], val[38]], 'column3', true);
        this.BaseTable.SET_CELL_UNEDITABLE([val[0], val[1], val[2], val[3], val[19], val[38]], 'column4', true);

        // 销售费用 10=11+12+13+14+15+16+17+18+19+27+28+29+30+31+32
        val[3].column3 = saleSum3 / 10000;
        val[3].column4 = saleSum4 / 10000;
        // 营业利润 18=10+11-13-14-15
        val[19].column3 = glSum3 / 10000;
        val[19].column4 = glSum4 / 10000;

        // 利润总额 27=18+19+22+23-25
        val[38].column3 = financeSum3 / 10000;
        val[38].column4 = financeSum4 / 10000;
        // 保存提交的数据
        this.saveData = {
          reportYear: this.year, // 提报年度
          reportMonth: this.month, // 提报月度
          // 销售费用
          byxsfy: val[3].column3,
          bnxsfy: val[3].column4,
          // 包装费
          bybzf: val[4].column3,
          bnbzf: val[4].column4,
          // 运输费
          byysf: val[5].column3,
          bnysf: val[5].column4,
          // 装卸费
          byzxf: val[6].column3,
          bnzxf: val[6].column4,
          //  仓储保管费
          byccbgf: val[7].column3,
          bnccbgf: val[7].column4,
          //  保险费
          byxsfybxf: val[8].column3,
          bnxsfybxf: val[8].column4,
          // 展览费
          byzlf: val[9].column3,
          bnzlf: val[9].column4,
          //  广告费
          byggf: val[10].column3,
          bnggf: val[10].column4,
          //  销售服务费
          byxsfwf: val[11].column3,
          bnxsfwf: val[11].column4,
          // 职工薪酬
          byxsfyzgxc: val[12].column3,
          bnxsfyzgxc: val[12].column4,
          //  业务经费
          byywjf: val[13].column3,
          bnywjf: val[13].column4,
          // 委托代销手续费
          bywtdxsxf: val[14].column3,
          bnwtdxsxf: val[14].column4,
          //  折旧费
          byzjf: val[15].column3,
          bnzjf: val[15].column4,
          //  修理费
          byxsfyglf: val[16].column3,
          bnxsfyglf: val[16].column4,
          // 样品及产品损耗
          byypjcpsh: val[17].column3,
          bnypjcpsh: val[17].column4,
          // 其他
          byxsfyqt: val[18].column3,
          bnxsfyqt: val[18].column4,
          // （二）管理费用
          byglfy: val[19].column3,
          bnglfy: val[19].column4,
          // 职工薪酬
          byglfyzgxc: val[20].column3,
          bnglfyzgxc: val[20].column4,
          // 保险费
          byglfybxf: val[21].column3,
          bnglfybxf: val[21].column4,
          // 折旧费
          byglfyzjf: val[22].column3,
          bnglfyzjf: val[22].column4,
          // 修理费
          byglfyxlf: val[23].column3,
          bnglfyxlf: val[23].column4,
          // 无形资产摊销
          bywxzctx: val[24].column3,
          bnwxzctx: val[24].column4,
          // 存货盘亏
          bychpk: val[25].column3,
          bnchpk: val[25].column4,
          // 业务招待费
          byywzdf: val[26].column3,
          bnywzdf: val[26].column4,
          // 差旅费
          byclf: val[27].column3,
          bnclf: val[27].column4,
          // 办公费
          bybgf: val[28].column3,
          bnbgf: val[28].column4,
          // 会议费
          byhyf: val[29].column3,
          bnhyf: val[29].column4,
          // 诉讼费
          byssf: val[30].column3,
          bnssf: val[30].column4,
          // 聘请中介机构费
          bypqzjjgf: val[31].column3,
          bnpqzjjgf: val[31].column4,
          //  咨询费
          byzxuf: val[32].column3,
          bnzxuf: val[32].column4,
          // 技术转让费
          byjszrf: val[33].column3,
          bnjszrf: val[33].column4,
          // 董事会费
          bydshf: val[34].column3,
          bndshf: val[34].column4,
          //  排污费
          bypwf: val[35].column3,
          bnpwf: val[35].column4,
          //  党建工作经费
          bydjgzjf: val[36].column3,
          bndjgzjf: val[36].column4,
          // 其他
          byglfyqt: val[37].column3,
          bnglfyqt: val[37].column4,
          // （三）财务费用
          bycwfy: val[38].column3,
          bncwfy: val[38].column4,
          // 其中： 利息支出
          bylxzc: val[39].column3,
          bnlxzc: val[39].column4,
          // 利息收入
          bylxsr: val[40].column3,
          bnlxsr: val[40].column4,
          //  汇兑净损失（收益以“－”列示）
          byhdjss: val[41].column3,
          bnhdjss: val[41].column4
        };
      }
      console.log(val, this.saveData, '---奥利给---');
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    changeHandle(params) {
      this.year = params.yearMonth.split('-')[0];
      this.month = params.yearMonth.split('-')[1];

      this.getList(this.year, this.month);
      this.columns[3].title = `月度：${this.year}年${this.month}月`;
    },
    resetChange(params) {
      this.year = params.yearMonth.split('-')[0];
      this.month = params.yearMonth.split('-')[1];

      this.getList(this.year, this.month);
      this.columns[3].title = `月度：${this.year}年${this.month}月`;
    },
    // 导入
    importHandle() {
      this.isImport = true;
      this.formData.name = '成本费用表导入';
      this.formData.upUrl = 'https://bjev-cms-oss-public.oss-cn-beijing.aliyuncs.com/成本费用表导入.xlsx';
      this.formData.importUrl = '/api/cmscloud.wholesales/cost/upload';
    },
    emitCloseHandler(val, type) {
      this.isImport = val;
      if (type === 'refresh') {
        // 重新加载
        this.getList(this.year, this.month);
      }
    },
    // 表单数据
    formChangeHandle(val) {
      this.saveData = Object.assign({}, this.saveData, val);
    },
    // 保存提交
    async dataHandle(status) {
      console.log(this.BaseTable.GET_REQUIRED_ERROR(), '---奥利给---');
      if (this.BaseTable.GET_REQUIRED_ERROR() != null) return;
      this.saveData.id = this.reportId ? this.reportId : ''; // Id
      this.saveData.costStatus = status; // 状态
      this.saveData.checkDesc = this.formList[0].initialValue;

      const res = await saveOne(this.saveData);
      if (res.resultCode == 200) {
        this.$notify.success({ title: '提示信息', message: '操作成功' });
        this.getList(this.year, this.month);
      }
    },
    // 查询列表
    async getList(Year = '', Month = '') {
      this.exportLoading = true;
      const res = await queryList({ reportYear: Year, reportMonth: Month });
      this.reportId = '';
      this.tableList = [];
      this.formList[0].initialValue = '';

      if (res.resultCode == 200) {
        if (res.data) {
          const data = res.data; // 表格

          this.reportId = data.id; // 导出传id
          this.currStatus = this.createDictText(data.costStatus, '6079');

          this.tableList = [
            {
              column1: '编制单位:',
              column2: data.dealerCode,
              column3: data.dealerName,
              column4: '单位：元'
            },
            {
              column1: '项    目',
              column2: '行次',
              column3: '本月数',
              column4: '本年累计数'
            },
            {
              column1: '四、期间费用',
              column2: '',
              column3: '',
              column4: ''
            },
            {
              column1: '（一）销售费用',
              column2: '10=11+12+13+14+15+16+17+18+19+27+28+29+30+31+32',
              column3: data.byxsfy,
              column4: data.bnxsfy
            },
            {
              column1: '1.包装费',
              column2: '11',
              column3: data.bybzf,
              column4: data.bnbzf
            },
            {
              column1: '2.运输费',
              column2: '12',
              column3: data.byysf,
              column4: data.bnysf
            },
            {
              column1: '3.装卸费',
              column2: '13',
              column3: data.byzxf,
              column4: data.bnzxf
            },
            {
              column1: '4.仓储保管费',
              column2: '14',
              column3: data.byccbgf,
              column4: data.bnccbgf
            },
            {
              column1: '5.保险费',
              column2: '15',
              column3: data.byxsfybxf,
              column4: data.bnxsfybxf
            },
            {
              column1: '6.展览费',
              column2: '16',
              column3: data.byzlf,
              column4: data.bnzlf
            },
            {
              column1: '7.广告费',
              column2: '17',
              column3: data.byggf,
              column4: data.bnggf
            },
            {
              column1: '8.销售服务费',
              column2: '18',
              column3: data.byxsfwf,
              column4: data.bnxsfwf
            },
            {
              column1: '9.职工薪酬',
              column2: '19',
              column3: data.byxsfyzgxc,
              column4: data.bnxsfyzgxc
            },
            {
              column1: '10.业务经费',
              column2: '27',
              column3: data.byywjf,
              column4: data.bnywjf
            },
            {
              column1: '11.委托代销手续费',
              column2: '28',
              column3: data.bywtdxsxf,
              column4: data.bnwtdxsxf
            },
            {
              column1: '12.折旧费',
              column2: '29',
              column3: data.byzjf,
              column4: data.bnzjf
            },
            {
              column1: '13.修理费',
              column2: '30',
              column3: data.byxsfyglf,
              column4: data.bnxsfyglf
            },
            {
              column1: '14.样品及产品损耗',
              column2: '31',
              column3: data.byypjcpsh,
              column4: data.bnypjcpsh
            },
            {
              column1: '15.其他',
              column2: '32',
              column3: data.byxsfyqt,
              column4: data.bnxsfyqt
            },
            {
              column1: '（二）管理费用',
              column2: '33=34+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58',
              column3: data.byglfy,
              column4: data.bnglfy
            },
            {
              column1: '1.职工薪酬',
              column2: '34',
              column3: data.byglfyzgxc,
              column4: data.bnglfyzgxc
            },
            {
              column1: '2.保险费',
              column2: '42',
              column3: data.byglfybxf,
              column4: data.bnglfybxf
            },
            {
              column1: '3.折旧费',
              column2: '43',
              column3: data.byglfyzjf,
              column4: data.bnglfyzjf
            },
            {
              column1: '4.修理费',
              column2: '44',
              column3: data.byglfyxlf,
              column4: data.bnglfyxlf
            },
            {
              column1: '5.无形资产摊销',
              column2: '45',
              column3: data.bywxzctx,
              column4: data.bnwxzctx
            },
            {
              column1: '6.存货盘亏',
              column2: '46',
              column3: data.bychpk,
              column4: data.bnchpk
            },
            {
              column1: '7.业务招待费',
              column2: '47',
              column3: data.byywzdf,
              column4: data.bnywzdf
            },
            {
              column1: '8.差旅费',
              column2: '48',
              column3: data.byclf,
              column4: data.bnclf
            },
            {
              column1: '9.办公费',
              column2: '49',
              column3: data.bybgf,
              column4: data.bnbgf
            },
            {
              column1: '10.会议费',
              column2: '50',
              column3: data.byhyf,
              column4: data.bnhyf
            },
            {
              column1: '11.诉讼费',
              column2: '51',
              column3: data.byssf,
              column4: data.bnssf
            },
            {
              column1: '12.聘请中介机构费',
              column2: '52',
              column3: data.bypqzjjgf,
              column4: data.bnpqzjjgf
            },
            {
              column1: '13.咨询费',
              column2: '53',
              column3: data.byzxuf,
              column4: data.bnzxuf
            },
            {
              column1: '14.技术转让费',
              column2: '54',
              column3: data.byjszrf,
              column4: data.bnjszrf
            },
            {
              column1: '15.董事会费',
              column2: '55',
              column3: data.bydshf,
              column4: data.bndshf
            },
            {
              column1: '16.排污费',
              column2: '56',
              column3: data.bypwf,
              column4: data.bnpwf
            },
            {
              column1: '17.党建工作经费',
              column2: '57',
              column3: data.bydjgzjf,
              column4: data.bndjgzjf
            },
            {
              column1: '18.其他',
              column2: '58',
              column3: data.byglfyqt,
              column4: data.bnglfyqt
            },
            {
              column1: '（三）财务费用',
              column2: '59=60+61+62',
              column3: data.bycwfy,
              column4: data.bncwfy
            },
            {
              column1: '其中： 利息支出',
              column2: '60',
              column3: data.bylxzc,
              column4: data.bnlxzc
            },
            {
              column1: '利息收入',
              column2: '61',
              column3: data.bylxsr,
              column4: data.bnlxsr
            },
            {
              column1: '汇兑净损失（收益以“－”列示）',
              column2: '62',
              column3: data.byhdjss,
              column4: data.bnhdjss
            }
          ];
          // 驳回意见赋值
          this.formList[0].initialValue = data.checkDesc;
          if (60791002 == data.costStatus) {
            this.isShow = false; // 已提交数据不展示按钮
            this.columns.map(x => {
              x.editable = false;
            });
          } else {
            this.isShow = true; // 未提交数据展示按钮
            this.columns.map(x => {
              x.editable = true;
            });
          }
        } else {
          this.isShow = true; // 未提交数据展示按钮
          this.currStatus = '';
          this.columns.map(x => {
            x.editable = true;
          });

          this.tableList = [
            {
              column1: '编制单位:',
              column2: getOwnerCode(),
              column3: this.dealerName,
              column4: '单位：元'
            },
            {
              column1: '项    目',
              column2: '行次',
              column3: '本月数',
              column4: '本年累计数'
            },
            {
              column1: '四、期间费用',
              column2: '',
              column3: '',
              column4: ''
            },
            {
              column1: '（一）销售费用',
              column2: '10=11+12+13+14+15+16+17+18+19+27+28+29+30+31+32',
              column3: 0,
              column4: 0
            },
            {
              column1: '1.包装费',
              column2: '11',
              column3: 0,
              column4: 0
            },
            {
              column1: '2.运输费',
              column2: '12',
              column3: 0,
              column4: 0
            },
            {
              column1: '3.装卸费',
              column2: '13',
              column3: 0,
              column4: 0
            },
            {
              column1: '4.仓储保管费',
              column2: '14',
              column3: 0,
              column4: 0
            },
            {
              column1: '5.保险费',
              column2: '15',
              column3: 0,
              column4: 0
            },
            {
              column1: '6.展览费',
              column2: '16',
              column3: 0,
              column4: 0
            },
            {
              column1: '7.广告费',
              column2: '17',
              column3: 0,
              column4: 0
            },
            {
              column1: '8.销售服务费',
              column2: '18',
              column3: 0,
              column4: 0
            },
            {
              column1: '9.职工薪酬',
              column2: '19',
              column3: 0,
              column4: 0
            },
            {
              column1: '10.业务经费',
              column2: '27',
              column3: 0,
              column4: 0
            },
            {
              column1: '11.委托代销手续费',
              column2: '28',
              column3: 0,
              column4: 0
            },
            {
              column1: '12.折旧费',
              column2: '29',
              column3: 0,
              column4: 0
            },
            {
              column1: '13.修理费',
              column2: '30',
              column3: 0,
              column4: 0
            },
            {
              column1: '14.样品及产品损耗',
              column2: '31',
              column3: 0,
              column4: 0
            },
            {
              column1: '15.其他',
              column2: '32',
              column3: 0,
              column4: 0
            },
            {
              column1: '（二）管理费用',
              column2: '33=34+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58',
              column3: 0,
              column4: 0
            },
            {
              column1: '1.职工薪酬',
              column2: '34',
              column3: 0,
              column4: 0
            },
            {
              column1: '2.保险费',
              column2: '42',
              column3: 0,
              column4: 0
            },
            {
              column1: '3.折旧费',
              column2: '43',
              column3: 0,
              column4: 0
            },
            {
              column1: '4.修理费',
              column2: '44',
              column3: 0,
              column4: 0
            },
            {
              column1: '5.无形资产摊销',
              column2: '45',
              column3: 0,
              column4: 0
            },
            {
              column1: '6.存货盘亏',
              column2: '46',
              column3: 0,
              column4: 0
            },
            {
              column1: '7.业务招待费',
              column2: '47',
              column3: 0,
              column4: 0
            },
            {
              column1: '8.差旅费',
              column2: '48',
              column3: 0,
              column4: 0
            },
            {
              column1: '9.办公费',
              column2: '49',
              column3: 0,
              column4: 0
            },
            {
              column1: '10.会议费',
              column2: '50',
              column3: 0,
              column4: 0
            },
            {
              column1: '11.诉讼费',
              column2: '51',
              column3: 0,
              column4: 0
            },
            {
              column1: '12.聘请中介机构费',
              column2: '52',
              column3: 0,
              column4: 0
            },
            {
              column1: '13.咨询费',
              column2: '53',
              column3: 0,
              column4: 0
            },
            {
              column1: '14.技术转让费',
              column2: '54',
              column3: 0,
              column4: 0
            },
            {
              column1: '15.董事会费',
              column2: '55',
              column3: 0,
              column4: 0
            },
            {
              column1: '16.排污费',
              column2: '56',
              column3: 0,
              column4: 0
            },
            {
              column1: '17.党建工作经费',
              column2: '57',
              column3: 0,
              column4: 0
            },
            {
              column1: '18.其他',
              column2: '58',
              column3: 0,
              column4: 0
            },
            {
              column1: '（三）财务费用',
              column2: '59=60+61+62',
              column3: 0,
              column4: 0
            },
            {
              column1: '其中： 利息支出',
              column2: '60',
              column3: 0,
              column4: 0
            },
            {
              column1: '利息收入',
              column2: '61',
              column3: 0,
              column4: 0
            },
            {
              column1: '汇兑净损失（收益以“－”列示）',
              column2: '62',
              column3: 0,
              column4: 0
            }
          ];
        }
      }else{
        this.currStatus = '';
      }
      this.exportLoading = false;
    },
    // 导出
    async exportHandle() {
      await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
        confirmButtonText: '下载',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.exportLoading = true;
          let param = { id: this.reportId };
          exportExcel(param)
            .then(response => {
              exportExcelFile(response.data, '成本费用表提报');
              this.exportLoading = false;
            })
            .catch(err => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
    },

    createdFrom() {
      return [
        {
          type: 'DATE',
          label: '月度',
          fieldName: 'yearMonth',
          dateType: 'month',
          initialValue: moment().format('YYYY-MM'),
          placeholder: '请选择'
        }
      ];
    },
    createdFrom1() {
      return [
        {
          type: 'TEXT_AREA',
          label: '驳回意见',
          fieldName: 'checkDesc',
          initialValue: '',
          selfCols: 3,
          placeholder: '',
          disabled: true
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '成本费用表',
          dataIndex: 'column1',
          filterType: 'input',
          align: 'center'
        },
        {
          title: '',
          dataIndex: 'column2',
          filterType: 'input',
          align: 'center'
        },
        {
          title: '',
          dataIndex: 'column3',
          filterType: 'input',
          align: 'center',
          editType: 'number',
          defaultEditable: true,
          editable: true,
          editRequired: true,
          msgtitle: '当前项',
          max: 999999999,
          min: -999999999
        },
        {
          title: `月度：${moment().format('YYYY')}年${moment().format('MM')}月`,
          dataIndex: 'column4',
          filterType: 'input',
          align: 'center',
          showOverflowTooltip: false,
          editType: 'number',
          defaultEditable: true,
          editable: true,
          editRequired: true,
          msgtitle: '当前项',
          max: 999999999,
          min: -999999999
        }
      ];
    }
  }
};
</script>
